package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.NjtCertificatePrint;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.domain.dto.NjtUserRegisterDto;
import com.ruoyi.nongjt.mapper.NjtCertificatePrintMapper;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.NjtCertificatePrintService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author seek
* @description 针对表【njt_certificate_print(合格证信息表)】的数据库操作Service实现
* @createDate 2025-09-26 11:13:41
*/
@Service
public class NjtCertificatePrintServiceImpl implements NjtCertificatePrintService {
    @Autowired
    private NjtCertificatePrintMapper njtCertificatePrintMapper;

    @Override
    public List<NjtCertificatePrint> getNjtCertificatePrintList(NjtCertificatePrint njtCertificatePrint) {
        return njtCertificatePrintMapper.selectNjtCertificatePrintList(njtCertificatePrint);
    }

    @Override
    public int updateNjtCertificatePrint(NjtCertificatePrint njtCertificatePrint) {
//        因为数据库有相应追踪字段(修改者，修改时间)
        njtCertificatePrint.setUpdateBy(SecurityUtils.getUsername());// 使用若依官方工具返回获取当前登录用户名
        njtCertificatePrint.setUpdateTime(new Date());
        return njtCertificatePrintMapper.updateCertificatePrint(njtCertificatePrint);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 声明事务，确保数据一致性
    public int deleteNjtCertificatePrintByIds(List<Long> ids) {
        return njtCertificatePrintMapper.deleteNjtCertificatePrintByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int registerCertificatePrint(NjtCertificatePrint njtCertificatePrint) {
        // 1. 参数校验
        if (njtCertificatePrint == null) {
            throw new ServiceException("参数不能为空");
        }
        if (njtCertificatePrint.getAccount() == null || njtCertificatePrint.getAccount().trim().isEmpty()) {
            throw new ServiceException("账号不能为空");
        }
        if (njtCertificatePrint.getProductionEntity() == null || njtCertificatePrint.getProductionEntity().trim().isEmpty()) {
            throw new ServiceException("生产主体不能为空");
        }
        if (njtCertificatePrint.getLegalPerson() == null || njtCertificatePrint.getLegalPerson().trim().isEmpty()) {
            throw new ServiceException("法人姓名不能为空");
        }
        if (njtCertificatePrint.getProductName() == null || njtCertificatePrint.getProductName().trim().isEmpty()) {
            throw new ServiceException("食用农产品名称不能为空");
        }

        // 2. 设置必要字段
        njtCertificatePrint.setCreateTime(new Date());
        njtCertificatePrint.setUpdateTime(new Date());
        njtCertificatePrint.setCreateUserId(SecurityUtils.getUserId());
        njtCertificatePrint.setPrintCount(0);    // 初始化打印次数
        njtCertificatePrint.setScanCount(0);      // 初始化扫描次数
        njtCertificatePrint.setIsDeleted(0);      // 0:未删除
        njtCertificatePrint.setDataSource("1");    // 默认数据来源

        // 3. 插入数据
        return njtCertificatePrintMapper.insertNjtCertificatePrint(njtCertificatePrint);
    }
}




